package com.example.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.mapper.CasesTypeMapper;
import com.example.demo.pojo.CasesType;
import com.example.demo.utils.LayuiResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/end/type")
public class EndTypeController {

    @Resource
    private CasesTypeMapper casesTypeMapper;


    @RequestMapping("/add")
    public Object add(@RequestBody CasesType casesType){
        casesTypeMapper.insert(casesType);
        return LayuiResult.addSuccess();
    }

    @RequestMapping("/delete/{id}")
    public Object delete(@PathVariable Integer id){
        CasesType casesType = new CasesType();
        casesType.setId(id);
        casesType.setStatus(0);
        casesTypeMapper.updateById(casesType);
        return LayuiResult.deleteSuccess();
    }

    @RequestMapping("/update")
    public Object update(@RequestBody CasesType casesType){
        casesTypeMapper.updateById(casesType);
        return LayuiResult.updateSuccess();
    }

    @RequestMapping("/get")
    public Object get(Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        QueryWrapper<CasesType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1);
        List<CasesType> casesTypes = casesTypeMapper.selectList(queryWrapper);
        Integer count = casesTypeMapper.selectCount(queryWrapper);
        PageInfo<CasesType> pageInfo = new PageInfo<>(casesTypes);
        return LayuiResult.selectSuccess(pageInfo.getList(),count);
    }

    @RequestMapping("/getAll")
    public Object getAll(){
        QueryWrapper<CasesType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1);
        List<CasesType> casesTypes = casesTypeMapper.selectList(queryWrapper);
        return LayuiResult.selectSuccess(casesTypes);
    }

    @RequestMapping("/search/{typeName}")
    public Object search(@PathVariable String typeName, Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        QueryWrapper<CasesType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1).like("type_name",typeName);
        List<CasesType> casesTypes = casesTypeMapper.selectList(queryWrapper);
        Integer count = casesTypeMapper.selectCount(queryWrapper);
        PageInfo<CasesType> pageInfo = new PageInfo<>(casesTypes);
        return LayuiResult.searchSuccess(pageInfo.getList(),count);
    }


}
